package com.zsummer.design.create.factorymethod;

/**
 * @author zsummer
 * 2020/3/22 10:05
 * 工厂方法
 */
public class Client {
    public static void main(String[] args) {
        PersonFactory factory = new PersonFactory();
        Person man = factory.getPerson("com.zsummer.design.create.factorymethod.Man");
        man.showSex();
        Person woman = factory.getPerson("com.zsummer.design.create.factorymethod.Woman");
        woman.showSex();
    }
}

abstract class Person {
    abstract void showSex();
}

class Man extends Person{

    @Override
    void showSex() {
        System.out.println("a man");
    }
}

class Woman extends Person{

    @Override
    void showSex() {
        System.out.println("a woman");
    }
}

class PersonFactory {
    public Person getPerson(String className){
        try {
            Class klass = Class.forName(className);
            return (Person) klass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}